//
//  BiPurchaseRefitCheckViewController.m
//  TrunkIndustryManageSystem
//
//  Created by WT_lyy on 15/4/28.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//
static CGRect Frame;

#import "BiPurchaseRefitCheckViewController.h"

#import "BiDatePickerView.h"
#import "BiPurchaseCarInfoView.h"

#import "BiPurchaseRefitEntity.h"
#import "BiPurChaseCarInfoEntity.h"
#import "BiPurchaseSupplier.h"

#import "BiPurchaseOilTankInfoView.h"
#import "BiPurchaseFlatTruckInfoView.h"
#import "BiPurchaseMixerTruckInfoView.h"
@interface BiPurchaseRefitCheckViewController ()<UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate,BiDatePickerViewDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate,UITextViewDelegate>{

    BiPurchaseCarInfoView * _viewCarInfo;
    
    __strong IBOutlet UIView *_viewBottom;
    
    __strong IBOutlet UIView *_viewMiddle;
    
    UITableView * _tabelViewFactory;
    
    UITableView * _tableViewVehiecle;
    
    NSMutableArray * _refitFactoryArr;
    
     BiDatePickerView * _datePickerView;
    
    NSInteger _timeType; //1,shangxian 2,xiaxian
    
    NSInteger _cellType; //1,改装厂 2为供应商
    
    NSInteger countIm;  //图片需上传计数
    NSInteger countUp; //图片上传完成计数
    
    NSMutableArray * _netImBtnArr; //网路图片按钮
    
    NSMutableArray * _locImBtnArr; //本地拍摄照片按钮
    
    NSMutableArray * _netImageObArr;
    
    BOOL isSave;
    
    NSInteger _index;  //纪录删除图片的索引
}


@property (weak, nonatomic) IBOutlet UILabel *labVin;
@property (weak, nonatomic) IBOutlet UILabel *labSaleState;
@property (weak, nonatomic) IBOutlet UILabel *laboprater;
@property (weak, nonatomic) IBOutlet UILabel *labcreadtime;
@property (weak, nonatomic) IBOutlet UITextField *textfPerformance;
@property (weak, nonatomic) IBOutlet UIButton *btnSppliser;
@property (weak, nonatomic) IBOutlet UIButton *btnBangd;
@property (weak, nonatomic) IBOutlet UIButton *btnUpTime;
@property (weak, nonatomic) IBOutlet UIButton *btnDownTime;
@property (weak, nonatomic) IBOutlet UILabel *labRefitFactory;

@property (nonatomic,strong) UIImage * imageContract;

@end

@implementation BiPurchaseRefitCheckViewController

- (id)init{

    self = [super init];
    
    if (self) {
        isSave = NO;
        
        _refitFactoryArr = [[NSMutableArray alloc] init];
        
        _netImBtnArr = [NSMutableArray new];
        
        _locImBtnArr = [NSMutableArray new];
        
        _netImageObArr = [NSMutableArray new];
        
        countIm = 0;
        countUp = 0;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavBarTitle:@"改装上线"];
    
    [self setNavBarWithHidden:NO];
    
    [self setNavBackBtn];
    
    _tableViewVehiecle = [[UITableView alloc] initWithFrame:CGRectMake(0, [self viewTop], MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT-40-[self viewTop])];
    
    _tableViewVehiecle.delegate = self;
    
    _tabelViewFactory.dataSource = self;
    
    _tabelViewFactory.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self _initVehTableHeadFooter];
    
    [self.view addSubview:_tableViewVehiecle];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];
    
    [self.view addGestureRecognizer:tap];
    
    [self dataFill];
}


- (void)_initVehTableHeadFooter{

    _viewCarInfo = [[[NSBundle mainBundle] loadNibNamed:@"BiPurchaseCarInfoView" owner:self options:nil] firstObject];
    
    _tableViewVehiecle.tableHeaderView = _viewCarInfo;
    
    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 280)];
    
    _viewBottom.frame = CGRectMake(0, 165, MAIN_SCREEN_WIDTH, 114);
    
    if (self.centity.imageArr.count>0) {
        
        footView.frame = CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 300+(MAIN_SCREEN_WIDTH-20)/3);
        
        _viewMiddle.frame = CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 165);
        
        _viewBottom.frame = CGRectMake(0, 165, MAIN_SCREEN_WIDTH, (MAIN_SCREEN_WIDTH-20)/3 + 135);
    }
    
    _tableViewVehiecle.tableFooterView = footView;
    
    [footView addSubview:_viewMiddle];
    
    [footView addSubview:_viewBottom];
}


- (void)dataFill{

    BiPurChaseCarInfoEntity * carInfo = self.centity.carEntity;
    
    [_viewCarInfo setCarInfo:carInfo withOrderNum:self.centity.manufacturerNum];
    
    self.labVin.text = carInfo.vin;
    
    self.labSaleState.text = carInfo.saleStatus;
    
    self.laboprater.text = self.centity.refitOnlineUserName.length==0?[BiUserEntity sharedInstance].name:self.centity.refitOnlineUserName;
    
    self.labcreadtime.text = [BiUtil stringFromFomate:[NSDate date] formate:@"yyyy-MM-dd hh:mm"];
    
    self.textfPerformance.text = self.centity.refitOnlinePerformance;
    
    [self.btnUpTime setTitle:self.centity.refitOnlineTime forState:UIControlStateNormal];
    
    [self.btnDownTime setTitle:self.centity.preRefitoutlineTime forState:UIControlStateNormal];
    
    self.labRefitFactory.text = self.centity.supplierName;
    
    if (self.centity.supplierName.length != 0) {
        
        float btnwidth = [GT_Tool widthForString:self.centity.supplierName fontSize:14 andHeitht:20];
        
        self.btnBangd.frame = CGRectMake(self.btnBangd.frame.origin.x, self.btnBangd.frame.origin.y, btnwidth+10, self.btnBangd.frame.size.height);
        [self.btnBangd setTitle:self.centity.systemNumber forState:UIControlStateNormal];
    }
    
    [_netImageObArr  addObjectsFromArray:self.centity.imageArr];
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    for (int i = 0; i < self.centity.imageArr.count; i++) {
        
        UIButton * imageViewtbn = [[UIButton alloc] initWithFrame:CGRectMake(5*(i+1)+i*(MAIN_SCREEN_WIDTH-20)/3, 40, (MAIN_SCREEN_WIDTH-20)/3, (MAIN_SCREEN_WIDTH-20)/3)];
        [imageViewtbn addTarget:self action:@selector(btnImageClcik:) forControlEvents:UIControlEventTouchUpInside];
        imageViewtbn.tag = 100+i;
        
        [_netImBtnArr addObject:imageViewtbn];
        
        [imageViewtbn reloadImage:self.centity.imageArr[i]];
        
        [_viewBottom addSubview:imageViewtbn];
        
    }

}

#pragma -amrk UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    Frame = _tableViewVehiecle.frame;
    _tableViewVehiecle.frame = CGRectMake(0, _tableViewVehiecle.frame.origin.y - 200, _tableViewVehiecle.frame.size.width, _tableViewVehiecle.frame.size.height);
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    _tableViewVehiecle.frame = Frame;
}

- (void)tapClick{

    [_textfPerformance resignFirstResponder];
}


#pragma -mark Btn Action

- (IBAction)btnOrangeClick:(UIButton *)sender {
    
    if (1 == sender.tag) {//gaizhuanghang
        
//        NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:[BiUserEntity sharedInstance].userid,@"id", nil];
//        
//        [[BiNetEngine shareInstance] requestPurchaseSppliserList:self paraDict:paraDic];
//        
//        [self asyshowHUDView:WAITING CurrentView:self];
        
    }else if (2 == sender.tag){//hetong
    
        NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:[BiUserEntity sharedInstance].userid,@"userId", self.centity.orderId,@"orderId",nil];
        
        [[BiNetEngine shareInstance] requestPurchaseContractList:self paraDict:paraDic];
        
        [self asyshowHUDView:WAITING CurrentView:self];
        
    }else if (3 == sender.tag){//hetong zhaopian
        if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"相机功能不可用" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alertView show];
            return;
        }
        
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:picker animated:YES completion:nil];
    
    }
}

- (IBAction)btnTimeClick:(UIButton *)sender {
    
    _timeType = sender.tag -10;
    
    if (!_datePickerView) {
        
        _datePickerView = [[BiDatePickerView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, self.view.frame.size.height)];
        
        [_datePickerView setDateModeandButton];
        
        _datePickerView.delegate = self;
    }
    
    [self.view addSubview:_datePickerView];
}

- (IBAction)btnSaveCheck:(UIButton *)sender {
    
    if (1 == sender.tag) { //保存
        
//        NSString * userId = nil;
//        
//        if ([self.centity.checkRukuUserId intValue] == 0) {
//            
//            userId = [BiUserEntity sharedInstance].userid;
//        }else{
//            
//            userId = self.centity.checkRukuUserId;
//        }
        
        NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:self.centity.e_id,@"id", self.centity.refitOnlineUserId,@"refitOnlineUserId",
            self.centity.refitOnlineCreateTime,@"refitOnlineCreateTime",
            self.centity.refitOnlinePerformance,@"refitOnlinePerformance",
            self.centity.refitOnlineSupplierId,@"refitOnlineSupplierId",
            self.centity.refitOnlineTime,@"refirOnlineTime",
            self.centity.preRefitoutlineTime,@"preRefiftoutlineTime",
            self.centity.uprefitId,@"uprefitId",nil];
        
        [[BiNetEngine shareInstance] requestPurchaseRefitSave:self paraDict:paraDic];
        
        
        for (int i = 0; i<_locImBtnArr.count; i++) {
            
            UIButton * imagebtn = _locImBtnArr[i];
            
            NSString * filename = [NSString stringWithFormat:@"%u",arc4random()+i];
            
            NSDictionary * paraDicIm1 = [NSDictionary dictionaryWithObjectsAndKeys:self.centity.e_id,@"id",filename,@"fileName", nil];
            
            [[BiNetEngine shareInstance] requestPurchaseRefitImage:self withImge:[imagebtn currentBackgroundImage] paraDic:paraDicIm1];
        }
        
        [self asyshowHUDView:WAITING CurrentView:self];
    }else if (2 == sender.tag){//审核
        
        NSDictionary * paraDic = [NSDictionary dictionaryWithObjectsAndKeys:self.centity.e_id,@"id", @"1",@"status",nil];
        
        [[BiNetEngine shareInstance] requestPurchaseRefitCheck:self paraDict:paraDic];
        
        [self asyshowHUDView:WAITING CurrentView:self];
    }
}

- (void)btnImageClcik:(UIButton *)button{

    UIActionSheet* mySheet = [[UIActionSheet alloc]
                              initWithTitle:nil
                              delegate:self
                              cancelButtonTitle:@"取消"
                              destructiveButtonTitle:@"删除图片"
                              otherButtonTitles:@"查看大图", nil];
    [mySheet showInView:self.view];
    
    mySheet.tag = button.tag;
}

#pragma mark -textview delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if ([@"\n" isEqualToString:text] == YES)
    {
        [textView resignFirstResponder];
        
        
        return NO;
    }
    return YES;
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSInteger index = actionSheet.tag%100;
    
    _index = index;
    
    switch (buttonIndex)
    {
        case 0:
        {  //删除图片
            
            if (actionSheet.tag < 200) { //网络图片
                
                NSDictionary * deleDic =[NSDictionary dictionaryWithObjectsAndKeys:self.centity.e_id,@"id",[_netImageObArr[index] lastPathComponent],@"filePath",@"10",@"imgType", nil];
                
                [_netImageObArr removeObjectAtIndex:index];
                
                [[BiNetEngine shareInstance] requestPuchaseRefitImageDel:self paraDic:deleDic];
                
                [self asyshowHUDView:WAITING CurrentView:self];
                
            }else{//本地拍照
                
                NSInteger  indexLoc = index - _netImBtnArr.count;
                
                UIButton * nowbutn = _locImBtnArr[indexLoc];
                
                [nowbutn removeFromSuperview];
                
                [_locImBtnArr removeObjectAtIndex:indexLoc];
                
                for (int i = indexLoc; i< _locImBtnArr.count;i++) {
                    
                    UIButton * butn = _locImBtnArr[i];
                    
                    butn.tag --;
                    
                    butn.frame = CGRectMake(butn.frame.origin.x-butn.frame.size.width-5, butn.frame.origin.y, butn.frame.size.width, butn.frame.size.height);
                }
                
            }
            if ((_netImBtnArr.count+_locImBtnArr.count) == 0) {
                    
                _viewMiddle.frame = CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 165);
                    
                _viewBottom.frame = CGRectMake(0, 165, MAIN_SCREEN_WIDTH,115);
                    
                UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 280)];
                    
                [footView addSubview:_viewMiddle];
                [footView addSubview:_viewBottom];
                    
                _tableViewVehiecle.tableFooterView = footView;

            }
        }
            break;
        case 1:
        {  // 查看大图
            
            if (actionSheet.tag < 200) { //网络图片
                
                [BiUtil pusuToImageVc:self image:[_netImBtnArr[index] currentBackgroundImage] title:@"合同照片"];
            }else{//本地拍照
                
                [BiUtil pusuToImageVc:self image:[_locImBtnArr[(index-_netImBtnArr.count)] currentBackgroundImage] title:@"合同照片"];
            }
            
        }
            break;
        default:
            break;
    }
}


#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    
    NSInteger Count = _netImBtnArr.count + _locImBtnArr.count;
    
    if (Count == 3) {
        
        [self showMessage:@"亲，最多三张图片！"];
        
        return;
    }
    
    image = [BiUtil scaleToSize:image size:CGSizeMake(240, 200)];
 
    UIButton * imageViewtbn = [[UIButton alloc] initWithFrame:CGRectMake(5*(Count+1)+Count*(MAIN_SCREEN_WIDTH-20)/3, 40, (MAIN_SCREEN_WIDTH-20)/3, (MAIN_SCREEN_WIDTH-20)/3)];
   
    [_viewBottom addSubview:imageViewtbn];
    
    if (0 == _netImBtnArr.count && _locImBtnArr.count == 0) {
        
        _viewMiddle.frame = CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 165);
        
        _viewBottom.frame = CGRectMake(0, 165, MAIN_SCREEN_WIDTH,135+(MAIN_SCREEN_WIDTH-20)/3);
        
        UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 300+(MAIN_SCREEN_WIDTH-20)/3)];
        
        [footView addSubview:_viewMiddle];
        [footView addSubview:_viewBottom];
        
        _tableViewVehiecle.tableFooterView = footView;

    }
    
    _tableViewVehiecle.contentSize = CGSizeMake(MAIN_SCREEN_WIDTH, _tableViewVehiecle.tableHeaderView.frame.size.height+_tableViewVehiecle.tableFooterView.frame.size.height);
    
    [imageViewtbn addTarget:self action:@selector(btnImageClcik:) forControlEvents:UIControlEventTouchUpInside];
    
    imageViewtbn.tag = 200+Count;
    
    [imageViewtbn setBackgroundImage:image forState:UIControlStateNormal];
     [_locImBtnArr addObject:imageViewtbn];
    
    countIm ++;
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}


#pragma -mark UitabeViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == _tableViewVehiecle) {
        if (self.centity.oilEntity) {
            
            return 276;
        }else if (self.centity.stinEntity){
            
            return 276;
        }else if (self.centity.flatEntity){
        
            return 346;
        }
        
    }else if (tableView == _tabelViewFactory){
    
        return 60;
    }

    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView == _tabelViewFactory) {
        
        [_tabelViewFactory removeFromSuperview];
        
        if (1 == _cellType) {
            float btnwidth = [GT_Tool widthForString:_refitFactoryArr[indexPath.row][@"name"] fontSize:14 andHeitht:20];
            
            self.btnSppliser.frame = CGRectMake(self.btnSppliser.frame.origin.x, self.btnSppliser.frame.origin.y, btnwidth+10, self.btnSppliser.frame.size.height);
            
            [self.btnSppliser setTitle:_refitFactoryArr[indexPath.row][@"name"] forState:UIControlStateNormal];
        }else if (2 == _cellType){
            
            BiPurchaseSupplier * supEntity = _refitFactoryArr[indexPath.row];
            
            float btnwidth = [GT_Tool widthForString:supEntity.systemNumber fontSize:14 andHeitht:20];
            
            self.btnBangd.frame = CGRectMake(self.btnBangd.frame.origin.x, self.btnBangd.frame.origin.y, btnwidth+10, self.btnBangd.frame.size.height);
            
            [self.btnBangd setTitle:supEntity.systemNumber forState:UIControlStateNormal];
            self.labRefitFactory.text = supEntity.supplierName;
        }
        
    }else if (tableView == _tableViewVehiecle){
    
    
        
    }

}

#pragma -mark UitabeViewDatasource

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == _tabelViewFactory) {
        
        if (_refitFactoryArr) {
            
            return _refitFactoryArr.count;
        }
    }else{
    
        if (self.centity.oilEntity) {
            
            return 1;
        }else if (self.centity.flatEntity){
        
            return 1;
        }else if (self.centity.stinEntity){
        
            return 1;
        }
    }
    
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell  * cell = nil;
    
    if (tableView == _tabelViewFactory) {
        
       cell = [self cellForSupplierRowAtIndexPath:indexPath];
    
        
    }else if (tableView == _tableViewVehiecle){
     
        static NSString * myidentify = @"VehiecleCell";
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myidentify];
        
        if (self.centity.oilEntity) {
            
            BiPurchaseOilTankInfoView * oilView = [[[NSBundle mainBundle] loadNibNamed:@"BiPurchaseOilTankInfoView" owner:self options:nil] lastObject];
            
            [oilView setOilDataFill:self.centity.oilEntity];
            
            [cell addSubview:oilView];
            
        }else if (self.centity.flatEntity) {
            
            BiPurchaseFlatTruckInfoView * flatView = [[[NSBundle mainBundle] loadNibNamed:@"BiPurchaseFlatTruckInfoView" owner:self options:nil] lastObject];
            
            [flatView setFlatDataFill:self.centity.flatEntity];
            
            [cell addSubview:flatView];
            
        }else if (self.centity.stinEntity) {
            
            BiPurchaseMixerTruckInfoView * mixeView = [[[NSBundle mainBundle] loadNibNamed:@"BiPurchaseMixerTruckInfoView" owner:self options:nil] lastObject];
            
            [mixeView setMixeDataFill:self.centity.stinEntity];
            
            [cell addSubview:mixeView];
            
        }
        
    }
    
    return cell;
}

- (UITableViewCell *)cellForSupplierRowAtIndexPath:(NSIndexPath *)indexPath{


    static NSString * myidentify = @"RefitFacCell";
    
    UITableViewCell * cell = [_tabelViewFactory dequeueReusableCellWithIdentifier:myidentify];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myidentify];
        
        UILabel * lab = GET_LABEL(CGRectMake(100, 10, 230, 16), 14, NO, [UIColor blackColor], NSTextAlignmentLeft);
        
        [cell addSubview:lab];
        
        lab.tag = 1111;
        
        UILabel * labsys = GET_LABEL(CGRectMake(10, 10, 100, 16), 14, NO, [UIColor blackColor], NSTextAlignmentLeft);
        labsys.text = @"绑定合同编号:";
        [cell addSubview:labsys];
        
        UILabel * labsna = GET_LABEL(CGRectMake(10, 30, 50, 16), 14, NO, [UIColor blackColor], NSTextAlignmentLeft);
        labsna.text = @"改装厂:";
        [cell addSubview:labsna];
        
        
        UILabel * labname = GET_LABEL(CGRectMake(62, 30, 250, 16), 14, NO, [UIColor blackColor], NSTextAlignmentLeft);
        
        [cell addSubview:labname];
        
        labname.tag = 1112;
    }
    
    UILabel * lab = (UILabel *)[cell viewWithTag:1111];
    UILabel * labname = (UILabel *)[cell viewWithTag:1112];
    
    
    if (1 == _cellType) {
        lab.text = _refitFactoryArr[indexPath.row][@"name"];
    }else if (2 == _cellType){
        BiPurchaseSupplier * supEntity = _refitFactoryArr[indexPath.row];
        
        lab.text = supEntity.systemNumber;
        labname.text = supEntity.supplierName;
    }
    
    return cell;
}

#pragma mark BiDatePickerViewDelegate
- (void)onDatePickerViewDateSelect:(id)sender date:(NSDate*)date
{
    [_datePickerView removeFromSuperview];
    
    NSString * dateStr = [BiUtil stringFromFomate:date formate:@"yyyy-MM-dd hh:mm:ss"];
    
    if (1 == _timeType) {
        
        [self.btnUpTime setTitle:dateStr forState:UIControlStateNormal];
    }else if (2 == _timeType){
    
        [self.btnDownTime setTitle:dateStr forState:UIControlStateNormal];
    }
    
}

#pragma -mark Net Delegate
- (void)onNetEngineResponse:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    
    
    NSDictionary * entity_ = (NSDictionary *)entity;
    
    [self showMessage:entity_[@"result_info"]];
    
    if ([entity_[@"response_status"] integerValue] != 200) {
        
         [self removeHUDView:self];
        
        return;
        
    }
    
    switch (tag) {
        case NET_GET_PURCHASE_SPPLIER_LIST:

            break;
        case NET_GET_PURCHASE_CONTRACT_LIST:
        {
            
             [self removeHUDView:self];
            id  contractData = entity_[@"data"];
            
            if ([contractData isKindOfClass:[NSArray class]]) {
                NSArray * conArr = (NSArray *)contractData;
                
                if (0 == conArr.count) {
                    [self showMessage:@"暂无数据！"];
                    
                    return;
                }else{
                
                    _cellType = 2;
                    
                    [_refitFactoryArr removeAllObjects];
                    
                    [_refitFactoryArr addObjectsFromArray:conArr];
                    
                    if (!_tabelViewFactory) {
                        _tabelViewFactory = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT)];
                        _tabelViewFactory.delegate = self;
                        _tabelViewFactory.dataSource = self;
                        
                    }
                    
                    [self.view.window addSubview:_tabelViewFactory];
                    _tabelViewFactory.tableHeaderView = [[UIView alloc] init];
                    [_tabelViewFactory reloadData];
                }
                
            }else{
            //绑定合同列表处理
                [self showMessage:@"暂无数据！"];
                
                return;
            }
        
        }

            break;
        case NET_GET_PURCHASE_REFIT_CHECK:
        {
            isSave = NO;
            [self.navigationController popViewControllerAnimated:YES];
            
        }

        case NET_GET_PURCHASE_REFIT_SAVE:
            isSave = YES;
            break;
        case NET_GET_PURCHASE_REFIT_CONTRACT_IMAGE:
            
            countUp++;
            break;
        case NET_GET_PURCHASE_REFIT_IMAGEDEL:
        {
            
            UIButton * nowbtn = _netImBtnArr[_index];
            
            [nowbtn removeFromSuperview];
            
            [_netImBtnArr removeObjectAtIndex:_index];
            
            
            for (int i = _index; i< _netImBtnArr.count;i++) {
                
                UIButton * butn = _netImBtnArr[i];
                
                butn.tag --;
                
                butn.frame = CGRectMake(butn.frame.origin.x-butn.frame.size.width-5, butn.frame.origin.y, butn.frame.size.width, butn.frame.size.height);
            }
            
            for (int i = 0; i< _locImBtnArr.count;i++) {
                
                UIButton * butn = _locImBtnArr[i];
                
                butn.tag --;
                
                butn.frame = CGRectMake(butn.frame.origin.x-butn.frame.size.width-5, butn.frame.origin.y, butn.frame.size.width, butn.frame.size.height);
            }
            if ((_netImBtnArr.count+_locImBtnArr.count) == 0) {
                
                _viewMiddle.frame = CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 165);
                
                _viewBottom.frame = CGRectMake(0, 165, MAIN_SCREEN_WIDTH,115);
                
                UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 280)];
                
                [footView addSubview:_viewMiddle];
                [footView addSubview:_viewBottom];
                
                _tableViewVehiecle.tableFooterView = footView;
                
            }
            
            [self removeHUDView:self];
        }
            break;
        default:
            break;
    }
    
    if ((countUp == countIm)&&isSave) {
        
        [self removeHUDView:self];
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}
- (void)onNetEngineResponseFail:(int)tag entity:(id)entity userInfo:(NSDictionary *)userInfo
{
    [self removeHUDView:self];
    
}


@end
